//
//  MPLoginViewController.m
//  SearchPeople
//
//  Created by YoloMao on 16/5/9.
//
//

#import "MPLoginViewController.h"
#import "MPImport.h"
#import "MPUser.h"

@interface MPLoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *usernameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;

@end

@implementation MPLoginViewController

#pragma mark - UIViewController Life Cycle

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - Action

- (IBAction)loginButtonClick:(id)sender
{
    if (![[MPSettingsManager getInstance] isValidateMobile:self.usernameTextField.text])
    {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号"];
        return;
    }
    if (![[MPSettingsManager getInstance] judgePassword:self.passwordTextField.text])
    {
        [SVProgressHUD showErrorWithStatus:@"密码为6-18位的数字或字母组合"];
        return;
    }
    [[MPDocumentHttpClient shareInstance] LoginWithUsername:self.usernameTextField.text
                                               WithPassword:self.passwordTextField.text
                                           WithSuccessBlock:^(MPRequest *request, MPResponse *response)
    {
        [SVProgressHUD showSuccessWithStatus:@"登录成功"];
        [self saveUserInfo:response.result];
        [self.navigationController popViewControllerAnimated:YES];

    } WithFailedBlock:^(MPRequest *request, MPResponse *response)
    {
        [SVProgressHUD showErrorWithStatus:@"登录失败"];
    }];
}

#pragma mark - Private method

- (void)saveUserInfo:(NSDictionary *)userData
{
    MPUser *user = [MPUser mj_objectWithKeyValues:userData];
    NSDictionary *data = [user mj_keyValues];
    [[MPSettingsManager getInstance].userDefault setObject:data forKey:@"user"];
}

@end
